<?php
class SiteManager_ArticlesController extends SiteManagerController
{

   	public function indexAction() {
   		$iPage = intval($this->getRequest()->getParam('page', 1));
    	$this->view->articles = Obj_Articles::getAllArticles($iPage);
    }

   	public function editAction() {
   	   	$id = (int) $this->getRequest()->getParam('id', 0);
		$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
		$arArticles = Obj_Articles::getOneArticle($id, false);
		if ($addnew || $arArticles === false) {
    		$this->view->articles = array(
	    		'ID' 			=> 0,
	    		'Title' 		=> '',
	    		'Content' 		=> '',
	    		'active' 		=> 0,
	    		'author' 		=> '',
	    		'seoTitle' 		=> '',
	    		'seoKeywords' 	=> '',
	    		'seoDescription'=> '',
	    		'date' 			=> ''
    		);
    	}
    	else {
			$arArticles['Content'] = stripslashes($arArticles['Content']);
    		$this->view->articles = $arArticles;
    	}
    }

    public function saveAction() {
		if (!$this->_isAJAX) {
				throw new Zend_Exception(_('Эта функция Вам недоступна'));
		}
		$id = (int) $this->getRequest()->getParam('ID', 0);
		$arArticles = array();
		$arArticles['Title'] = trim($this->getRequest()->getParam('Title', ''));
		$arArticles['Content'] = trim($this->getRequest()->getParam('Content', ''));
		$arArticles['active'] = (int)intval($this->getRequest()->getParam('active', 0));
		$arArticles['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
		$arArticles['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
		$arArticles['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
		$arArticles['date'] = date("Y-m-d H:i:s", time());
		$arArticles['alias'] = Obj_Articles::getAlias($arArticles['Title'], $id);
        $arArticles['author'] = 0;
    	if (Zend_Auth::getInstance()->hasIdentity()) {
	    	$user = Zend_Auth::getInstance()->getIdentity();
    		$arArticles['author'] = $user->ID;
    	}
        $allArticles = Obj_Articles::getAllArticles(1);
		$errors = array('is_error' => 0, 'err_string' => '');
        if($id != 0){
			$current_article = Obj_Articles::getOneArticle($id);
			$current_article['Content'] = stripslashes($current_article['Content']);
			if(	
				$arArticles['Title'] == $current_article['Title'] &&
				$arArticles['Content'] == $current_article['Content'] && 
				$arArticles['active'] == $current_article['active'] && 
				$arArticles['seoTitle'] == $current_article['seoTitle'] && 
				$arArticles['seoDescription'] == $current_article['seoDescription'] && 
				$arArticles['seoKeywords'] == $current_article['seoKeywords']
			){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Даная статья уже существует. Нет необходимости в повторном добавлении!\n";
			}

			if($arArticles['Title'] == ''){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Задайте заголовок страницы!\n";
			}
			if(strlen($arArticles['Title']) > 254){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Заголовок страницы слишком длинный!\n";
			}
			if(strip_tags($arArticles['Content']) == ""){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Контент страницы не может быть пустым!\n";
			}
		} else {
			if($arArticles['Title'] == ''){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Задайте заголовок страницы!\n";
			}
			if(strlen($arArticles['Title']) > 254){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Заголовок страницы слишком длинный!\n";
			}
			if(trim(strip_tags($arArticles['Content'])) == ""){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Контент страницы не может быть пустым!\n";
			}
		}
		foreach ($allArticles as $v){
			if ($v['Title'] == $arArticles['Title'] && $id != 0 && $id != $v['ID']){
				$errors['is_error'] = 1;
				$errors['err_string'] .= "Статья з таким заголовком уже существует!\n";					
			}
		}
		if($errors['is_error'] == 1){
			$this->view->error = $errors['err_string'];
			$this->_helper->json(array('error' => 1, 'errorStr' => $errors['err_string']), true, false);
			return;
			die();
		}
		$aid = (int)Obj_Articles::saveArticle($arArticles, $id);
		if ($arArticles['active'] === 1){
    		Obj_System_SearchIndex::write($aid, CONTENT_TYPE_ARTICLES, $arArticles['Title'], strip_tags($arArticles['Content']));
    	} else {
    		Obj_System_SearchIndex::delete($aid, CONTENT_TYPE_ARTICLES);
    	} 
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller' => 'articles', 'action' => 'index'))), true, false);
    }

    public function deleteAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	Obj_Articles::delOneArticle($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_ARTICLES);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller' => 'articles', 'action' => 'index'))), true, false);
    }

    public function groupedDeleteAction(){
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arDelete = array();
    	$arDelete = $this->getRequest()->getParam('arDelete', null);
    	foreach ($arDelete as $k => $v){
	    	$id = (int) $v;
	    	Obj_Articles::delOneArticle($id);
	    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_ARTICLES);
    	}
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller' => 'articles', 'action' => 'index'))), true, false);
    }

    public function stateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arArticle = Obj_Articles::getOneArticle($id);
		$arArticle['Content'] = stripslashes($arArticle['Content']);
    	if(intval($arArticle['active']) === 1){
    		$arArticle['active'] = 0;
    		$aid = Obj_Articles::saveArticle($arArticle, $id);
    		Obj_System_SearchIndex::delete($aid, CONTENT_TYPE_ARTICLES);
    	} else {
    		$arArticle['active'] = 1;
    		$aid = Obj_Articles::saveArticle($arArticle, $id);
    		Obj_System_SearchIndex::write($aid, CONTENT_TYPE_ARTICLES, $arArticle['Title'], $arArticle['Content']);
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('module' => 'site-manager', 'controller' => 'articles', 'action' => 'index'))), true, false);
    }
}